package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageBatchListBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageOperatorBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageOverviewBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/9/5.
 */

public class RefundStorageUtils {

    //获取退库信息
    public static RefundStorageOverviewBean getRefundStorageOverviewBean(Context context) {
        String json = SPUtils.getRefundStorageInformation(context);
        if (TextUtils.isEmpty(json)) {
            return new RefundStorageOverviewBean();
        } else {
            Gson gson = new Gson();
            return gson.fromJson(json, RefundStorageOverviewBean.class);
        }
    }

    //保存退库信息
    public static void saveRefundStorageOverviewBean(Context context, RefundStorageOverviewBean refundStorageOverviewBean) {
        Gson gson = new Gson();
        SPUtils.saveRefundStorageInformation(context, gson.toJson(refundStorageOverviewBean, RefundStorageOverviewBean.class));
    }

    //过滤销售出库货品列表信息
    public static List<RefundStorageOverviewBean.DataBean.MaterielListBean> getRefundStorageMaterielList(Context context, String materiel) {
        List<RefundStorageOverviewBean.DataBean.MaterielListBean> refundStorageList = new ArrayList<>();
        List<RefundStorageOverviewBean.DataBean.MaterielListBean> allRefundStorageList = getRefundStorageOverviewBean(context).getData().getMaterielList();
        for (int i = 0; i < allRefundStorageList.size(); i++) {
            RefundStorageOverviewBean.DataBean.MaterielListBean item = allRefundStorageList.get(i);
            if (item.getMaterielCode().contains(materiel) || item.getMaterielDescription().contains(materiel)) {
                refundStorageList.add(item);
                continue;
            }
        }
        return refundStorageList;
    }

    //检查退库列表是否至少填写一项
    public static int getRefundStorageNumber(Context context) {
        int result = 0;
        List<RefundStorageOverviewBean.DataBean.MaterielListBean> materielList = getRefundStorageOverviewBean(context).getData().getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            RefundStorageOverviewBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (!TextUtils.isEmpty(materielItem.getRefundNumber()) && Float.parseFloat(materielItem.getRefundNumber()) > 0 && materielItem.getBatch() != null) {
                result = result + 1;
            }
        }
        return result;
    }

    //通过序号获取退库物料信息
    public static RefundStorageOverviewBean.DataBean.MaterielListBean getMaterielInformationBySerial(Context context, String serial) {
        List<RefundStorageOverviewBean.DataBean.MaterielListBean> list = getRefundStorageOverviewBean(context).getData().getMaterielList();
        for (int i = 0; i < list.size(); i++) {
            RefundStorageOverviewBean.DataBean.MaterielListBean item = list.get(i);
            if (serial.equals(item.getSerial())) {
                return item;
            }
        }
        return null;
    }

    //创建查看批次特性
    public static void createTextView(Context context, ViewGroup viewGroup, String name, String text) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_refund_storage_textview, null);
        TextView nameTextView = (TextView) view.findViewById(R.id.name);
        TextView textTextView = (TextView) view.findViewById(R.id.text);
        nameTextView.setText(name);
        textTextView.setText(text);
        viewGroup.addView(view);
    }

    //通过序列号更新物料信息
    public static void updateMaterielBySerial(Context context, String serial, String refundStorageNumber, RefundStorageBatchListBean.DataBean.BatchListBean batchItem) {
        RefundStorageOverviewBean bean = getRefundStorageOverviewBean(context);
        List<RefundStorageOverviewBean.DataBean.MaterielListBean> materielList = bean.getData().getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            RefundStorageOverviewBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getSerial().equals(serial)) {
                materielItem.setRefundNumber(refundStorageNumber);
                materielItem.setBatch(batchItem);
                materielList.remove(i);
                materielList.add(0, materielItem);
                break;
            }
        }
        bean.getData().setMaterielList(materielList);
        saveRefundStorageOverviewBean(context, bean);
    }

    //是否有销售出库操作人员
    public static boolean hasOperator(Context context) {
        List<RefundStorageOperatorBean.DataBean.ReceiverListBean> operatorList = getRefundStorageOverviewBean(context).getData().getOperatorList();
        if (operatorList != null && !operatorList.isEmpty()) {
            return true;
        }
        return false;
    }

    //删除退库验收人
    public static void deleteOperator(Context context, String id) {
        RefundStorageOverviewBean bean = getRefundStorageOverviewBean(context);
        List<RefundStorageOperatorBean.DataBean.ReceiverListBean> operatorList = bean.getData().getOperatorList();
        for (int i = 0; i < operatorList.size(); i++) {
            RefundStorageOperatorBean.DataBean.ReceiverListBean operatorItem = operatorList.get(i);
            if (operatorItem.getId().equals(id)) {
                operatorList.remove(operatorItem);
                break;
            }
        }
        bean.getData().setOperatorList(operatorList);
        saveRefundStorageOverviewBean(context, bean);
    }

    //添加退库验收人
    public static void addOperator(Context context, RefundStorageOperatorBean.DataBean.ReceiverListBean receiveItem) {
        RefundStorageOverviewBean bean = getRefundStorageOverviewBean(context);
        List<RefundStorageOperatorBean.DataBean.ReceiverListBean> receiverList = bean.getData().getOperatorList() == null ? new ArrayList<>() : bean.getData().getOperatorList();
        receiverList.add(0, receiveItem);
        bean.getData().setOperatorList(receiverList);
        saveRefundStorageOverviewBean(context, bean);
    }

    //拼销售出库对象
    public static RefundStorageConfirmRequestBean getRefundStorageConfirmRequestBean(Context context, String voucherDate, String postDate) {
        RefundStorageConfirmRequestBean result = new RefundStorageConfirmRequestBean();
        RefundStorageOverviewBean bean = getRefundStorageOverviewBean(context);
        result.setRefundNumber(bean.getData().getRefundNumber());
        result.setPickingNumber(bean.getData().getPickingNumber());
        result.setVoucherDate(voucherDate);
        result.setPostDate(postDate);
        //操作人
        List<RefundStorageConfirmRequestBean.OperatorListBean> resultOperatorList = new ArrayList<>();
        List<RefundStorageOperatorBean.DataBean.ReceiverListBean> operatorList = bean.getData().getOperatorList();
        for (int i = 0; i < operatorList.size(); i++) {
            RefundStorageConfirmRequestBean.OperatorListBean operator = new RefundStorageConfirmRequestBean.OperatorListBean();
            operator.setId(operatorList.get(i).getId());
            resultOperatorList.add(operator);
        }
        result.setOperatorList(resultOperatorList);
        //物料
        List<RefundStorageConfirmRequestBean.MaterielListBean> resultMaterielList = new ArrayList<>();
        List<RefundStorageOverviewBean.DataBean.MaterielListBean> materielList = bean.getData().getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            RefundStorageConfirmRequestBean.MaterielListBean resultMateriel = new RefundStorageConfirmRequestBean.MaterielListBean();
            RefundStorageOverviewBean.DataBean.MaterielListBean materiel = materielList.get(i);
            resultMateriel.setSerial(materiel.getSerial());
            resultMateriel.setNumber(materiel.getRefundNumber());
            if (materiel.getBatch() != null) {
                resultMateriel.setBatchNumber(materiel.getBatch().getBatchNumber());
            }
            if (!TextUtils.isEmpty(resultMateriel.getNumber()) && !TextUtils.isEmpty(resultMateriel.getBatchNumber())) {
                resultMaterielList.add(resultMateriel);
            }
        }
        result.setMaterielList(resultMaterielList);
        return result;
    }

    public static void updateRefundStorageNumber(Context context, String refundNumber) {
        RefundStorageOverviewBean bean = getRefundStorageOverviewBean(context);
        bean.getData().setRefundNumber(refundNumber);
        saveRefundStorageOverviewBean(context, bean);
    }

}
